package com.joysuch.wwyt.api.mh.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.zlg.common.util.CollectionUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.joysuch.wwyt.api.mh.entity.MonitorPointAPIData;
import com.joysuch.wwyt.api.mh.mapper.MonitorPointDataApiMapper;
import com.joysuch.wwyt.api.mh.repository.MonitorPointDataAPIRepository;
import com.joysuch.wwyt.api.mh.service.MonitorPointDataAPIService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.mh.storage.MonitorPointData;
import com.joysuch.wwyt.util.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class MonitorPointAPIDataServiceImpl implements MonitorPointDataAPIService {



    @Autowired
    private MonitorPointDataAPIRepository monitorPointDataAPIRepository;


    @Override
    public List<Map<String, Object>> findAll(Long tenentId, String orgCode) {
        List<Map<String, Object>> list = new ArrayList<>();
        Calendar now = Calendar.getInstance();
        Date endTime = now.getTime();
        now.add(Calendar.MINUTE, -5);
        Date startTime = now.getTime();
        List<MonitorPointAPIData> dataList = monitorPointDataAPIRepository.findAllByTenentIdAndOrgCode(tenentId,orgCode,startTime);
        log.debug("read size {}", dataList.size());
        List<MonitorPointData> monitorPointData = new ArrayList<>();
        for(MonitorPointAPIData data: dataList){
            if(data.getTagValue() == null){
                log.warn("MH monitor point value is null from api: point name="+ data.getTagName());
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("tag", data.getTagName());
            List<LinkedHashMap<String, Object>> values =new ArrayList<>();
            LinkedHashMap<String, Object> valOne =new LinkedHashMap<>();
            valOne.put("time", data.getTagTime().getTime());
            valOne.put("value", data.getTagValue());
            valOne.put("remark", data.getRemark());
            valOne.put("type", data.getPointType());
            valOne.put("unit", data.getPointUnit());
            valOne.put("orgCode", data.getOrgCode());
            valOne.put("tenentId", data.getTenentId());
            valOne.put("stationId", data.getStationId());
            values.add(valOne);
            map.put("values", values);
            list.add(map);
        }
        return list;
    }

    @Override
    public void pushMonitorPointData(List<MonitorPointAPIData> dataList , Long tenantId, String orgCode) {

            if (CollectionUtils.isNotEmpty(dataList)) {
                for (MonitorPointAPIData monitorPointAPIData : dataList) {
                    MonitorPointAPIData monitorPointAPIData1 = monitorPointDataAPIRepository.findOneByTagName(monitorPointAPIData.getTagName(), tenantId, orgCode);
                    if (null != monitorPointAPIData1) {
                        BeanUtil.copyProperties(monitorPointAPIData, monitorPointAPIData1,"tagId");
                        monitorPointAPIData1.setTenentId(tenantId.toString());
                        monitorPointAPIData1.setOrgCode(orgCode);
                        monitorPointDataAPIRepository.save(monitorPointAPIData1);
                    } else {
                        monitorPointAPIData.setTagId(SnowFlakeUtil.getTagId());
                        monitorPointAPIData.setTenentId(tenantId.toString());
                        monitorPointAPIData.setOrgCode(orgCode);
                        monitorPointDataAPIRepository.save(monitorPointAPIData);

                    }
                }
            }

    }
}
